﻿Imports Hinshelwood.TFSStickyBuddy.Modules.Interfaces
Imports System.ComponentModel
Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports Microsoft.Practices.Composite.Wpf.Commands
Imports Hinshelwood.TFSStickyBuddy.HierarchicalWorkItems
Imports System.Collections.ObjectModel
Imports QuantumBitDesigns.Core

Namespace PresentationModels


    Public Class TreeByPresentationModel
        Implements ITreeByPresentationModel
        Implements INotifyPropertyChanged

        Private m_ItemWrappers As New ItemCollection
        'Private m_WorkItems As New ObservableCollection(Of WorkItem)

        Private ReadOnly m_Queries As ObservableList(Of StoredQuery)
        Private ReadOnly m_SelectStoredQueryCommand As DelegateCommand(Of StoredQuery)
        Private ReadOnly m_RefreshDataCommand As DelegateCommand(Of Project)

        Private m_TreeType As Node.TreeType
        Private m_TeamProject As Project
        Private m_Query As StoredQuery

        Public ReadOnly Property ItemWrappers() As ItemCollection Implements Interfaces.ITreeByPresentationModel.ItemWrappers
            Get
                Return m_ItemWrappers
            End Get
        End Property

        Public ReadOnly Property Title() As String
            Get
                Return String.Format("{0} on {1}", DisplayBy.ToString, m_TeamProject.Name)
            End Get
        End Property


        Public Property DisplayBy() As Node.TreeType Implements ITreeByPresentationModel.DisplayBy
            Get
                Return m_TreeType
            End Get
            Set(ByVal value As Node.TreeType)
                If Not m_TreeType = value Then
                    m_TreeType = value
                    OnPropertyChanged("DisplayBy")
                    OnPropertyChanged("Title")
                End If
            End Set
        End Property

        Public Property TeamProject() As Project Implements Interfaces.ITreeByPresentationModel.TeamProject
            Get
                Return m_TeamProject
            End Get
            Set(ByVal value As Project)
                If Not m_TeamProject Is value Then
                    m_TeamProject = value
                    OnPropertyChanged("TeamProject")
                    OnPropertyChanged("Title")
                    LoadStoredQuerys()
                End If
            End Set
        End Property

        Public Property Query() As StoredQuery Implements Interfaces.ITreeByPresentationModel.Query
            Get
                Return m_Query
            End Get
            Set(ByVal value As StoredQuery)
                If Not m_Query Is value Then
                    m_Query = value
                    OnPropertyChanged("Query")
                End If
            End Set
        End Property

        Public ReadOnly Property Queries() As ObservableCollection(Of StoredQuery) Implements ITreeByPresentationModel.Queries
            Get
                Return m_Queries.ObservableCollection
            End Get
        End Property

        Public ReadOnly Property RefreshDataCommand() As Microsoft.Practices.Composite.Wpf.Commands.DelegateCommand(Of Project) Implements Interfaces.ITreeByPresentationModel.RefreshDataCommand
            Get
                Return m_RefreshDataCommand
            End Get
        End Property

        Public ReadOnly Property SelectStoredQueryCommand() As Microsoft.Practices.Composite.Wpf.Commands.DelegateCommand(Of Microsoft.TeamFoundation.WorkItemTracking.Client.StoredQuery) Implements Interfaces.ITreeByPresentationModel.SelectStoredQueryCommand
            Get
                Return m_SelectStoredQueryCommand
            End Get
        End Property

        Public Sub New()
            m_Queries = New ObservableList(Of StoredQuery)(Application.Current.Dispatcher)
            m_SelectStoredQueryCommand = New DelegateCommand(Of StoredQuery)(AddressOf OnSelectStoredQueryExecuted, AddressOf OnSelectStoredQueryCanExecute)
            m_RefreshDataCommand = New DelegateCommand(Of Project)(AddressOf OnRefreshDataExecuted, AddressOf OnRefreshDataCanExecute)
        End Sub

        Public Sub OnSelectStoredQueryExecuted(ByVal StoredQuery As StoredQuery)
            m_Query = StoredQuery
        End Sub

        Public Function OnSelectStoredQueryCanExecute(ByVal StoredQuery As StoredQuery) As Boolean
            Return Not TeamProject Is Nothing AndAlso Queries.Count > 0
        End Function

        Public Sub OnRefreshDataExecuted(ByVal TeamProject As Project)
            LoadStoredQuerys()
            m_ItemWrappers.Clear()

            Dim Import As New TFSImport
            Import.Import(m_ItemWrappers, TeamProject, Query, DisplayBy)

        End Sub

        Public Function OnRefreshDataCanExecute(ByVal TeamProject As Project) As Boolean
            Return Not TeamProject Is Nothing
        End Function


        Private Sub LoadStoredQuerys()
            If Not TeamProject Is Nothing Then
                m_Queries.Clear()
                For Each x In TeamProject.StoredQueries
                    m_Queries.Add(x)
                Next
                If Query Is Nothing Then
                    Query = m_Queries(0)
                End If
                SelectStoredQueryCommand.RaiseCanExecuteChanged()
                RefreshDataCommand.RaiseCanExecuteChanged()
            End If
        End Sub


#Region " INotifyPropertyChanged "

        Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

        Public Sub OnPropertyChanged(ByVal PropertyName As String)
            If Not Application.Current.Dispatcher.CheckAccess Then
                Application.Current.Dispatcher.Invoke(New Action(Of String)(AddressOf OnPropertyChanged), PropertyName)
            End If
            If Not PropertyChangedEvent Is Nothing Then
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(PropertyName))
                SelectStoredQueryCommand.RaiseCanExecuteChanged()
                RefreshDataCommand.RaiseCanExecuteChanged()
            End If
        End Sub

#End Region



    End Class

End Namespace